package com.taniurakengo.sourceviewer;

import com.taniurakengo.sourceviewer.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;

/**
 * 
 * @author Taniura
 *
 */
public class SettingsPreference extends PreferenceActivity {

	//
	private static final boolean ALWAYS_SIMPLE_PREFS = false;

	/**
	 * 
	 */
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);

		setupSimplePreferencesScreen();
	}

	/**
	 * 
	 */
	@SuppressWarnings("deprecation")
	private void setupSimplePreferencesScreen() {
		if (!isSimplePreferences(this)) {
			return;
		}

		addPreferencesFromResource(R.xml.pref_general);
		setSummary();
	}

	/**
	 * 
	 */
	@Override
	public boolean onIsMultiPane() {
		return isXLargeTablet(this) && !isSimplePreferences(this);
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	private static boolean isXLargeTablet(Context context) {
		return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	private static boolean isSimplePreferences(Context context) {
		return ALWAYS_SIMPLE_PREFS
				|| Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
				|| !isXLargeTablet(context);
	}

	private SharedPreferences.OnSharedPreferenceChangeListener listener =   
		    new SharedPreferences.OnSharedPreferenceChangeListener() {  

		public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,  String key) {
			setSummary();
		}
		
	};

	private void setSummary(){
		setChecboxSummary("BRUSH_C++");
		setChecboxSummary("BRUSH_C#");
		setChecboxSummary("BRUSH_CSS");
		setChecboxSummary("BRUSH_JAVA");
		setChecboxSummary("BRUSH_PERL");
		setChecboxSummary("BRUSH_PHP");
		setChecboxSummary("BRUSH_PLAIN");
		setChecboxSummary("BRUSH_PYTHON");
		setChecboxSummary("BRUSH_RUBY");
		setChecboxSummary("BRUSH_SCALE");
		setChecboxSummary("BRUSH_SQL");
		setChecboxSummary("BRUSH_VB");
		setChecboxSummary("BRUSH_XML");
		setChecboxSummary("BRUSH_ALL");
		
		ListPreference list_preference = (ListPreference)getPreferenceScreen().findPreference("CHARSET");
		list_preference.setSummary("      " + list_preference.getValue());

		list_preference = (ListPreference)getPreferenceScreen().findPreference("CSS_STYLE");
		list_preference.setSummary("      " + list_preference.getValue());

	}
	
	private void setChecboxSummary(CharSequence key){
		CheckBoxPreference checkbox_preference = (CheckBoxPreference)getPreferenceScreen().findPreference(key);
		if (checkbox_preference.isChecked()) {
		    checkbox_preference.setSummary("      ture");
		} else {
		    checkbox_preference.setSummary("      false");
		}
	}
	
	@Override
	protected void onResume() {
	    super.onResume();
	    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(listener);
	}
	 
	@Override
	protected void onPause() {
	    super.onPause();
	    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(listener);
	}
}
